package com.tencent.sr.iris.activity.service.event.activity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityCheckResultDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.service.event.task.IrisTaskExecutor;
import com.tencent.sr.iris.activity.service.event.task.UserLevelUpPromoTaskExecutor;
import com.tencent.sr.iris.user.core.service.interfaces.enums.UserLevelEnum;
import com.tencent.sr.iris.user.core.service.interfaces.request.message.UserActivityLevelUpRequest;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 升级有礼(晋升奖)
 *
 * @author xiaojun
 *         注：当前活动处理器的对象名称是 活动类型ActivityEventTypeEnum的code
 */
@Slf4j
@Component
public class UserLevelUpPromoActivityExecutor extends AbstractIrisActivityExecutor {

    @Resource
    private UserLevelUpPromoTaskExecutor userLevelUpPromoTaskExecutor;

    @Override
    protected void process(ActivityEventDTO msg, ActivityDTO activity, List<String> eventHandingInfoList) {
        // 4.活动准入规则校验
        ActivityCheckResultDTO activityFlag = doActivityCheck(activity, msg);
        if (!activityFlag.isSatisfy()) {
            log.info("不满足活动准入规则,activityId={},msg={}", activity, JSONObject.toJSONString(msg));
            eventHandingInfoList.add("不满足活动准入规则");
            return;
        }

        // 循环执行所有任务
        String eventInfo = msg.getEventInfo();
        UserActivityLevelUpRequest levelUpRequest = JSON.parseObject(eventInfo, UserActivityLevelUpRequest.class);
        if (Objects.isNull(levelUpRequest) || StringUtils.isEmpty(levelUpRequest.getAfterLevelId())) {
            log.info("消息体为空 activity:{} msg:{}", JsonUtils.toJsonString(activity), JsonUtils.toJsonString(msg));
            eventHandingInfoList.add("消息体为空");
            return;
        }

        for (ActivityTaskDTO activityTaskDTO : activity.getActivityTaskDTOS()) {
            if (levelUpRequest.getAfterLevelId().equals(activityTaskDTO.getBizId())) {

                // 获取任务执行器
                IrisTaskExecutor taskExecutor = getTaskExecutor(msg, activity, activityTaskDTO);
                taskExecutor.execute(msg, activity, activityTaskDTO, null);
            }
        }
        activityRecordService.addOrUpdateActivityRecord(msg.getUid(), msg.getEventTime(), activity.getBizId(),
                activity.getPeriodType(), activity.getPeriod());
    }

    @Override
    protected void after(ActivityEventDTO eventDTO, ActivityDTO act) {

    }

    @Override
    protected List<ActivityTaskDTO> getTask(ActivityEventDTO eventDTO, ActivityDTO act) {
        return null;
    }


    @Override
    protected void check(ActivityEventDTO msg, ActivityDTO activity) {
    }

    @Override
    public ActivityCheckResultDTO doActivityCheck(ActivityDTO activity, ActivityEventDTO msg) {
        String eventInfo = msg.getEventInfo();
        UserActivityLevelUpRequest levelUpRequest = JSON.parseObject(eventInfo, UserActivityLevelUpRequest.class);
        if (Objects.isNull(levelUpRequest)) {
            return new ActivityCheckResultDTO(false, "请求体为空");
        }
        // 只有一级级升的才有奖励金
        boolean b = UserLevelEnum.judgeNextLevel(levelUpRequest.getBeforeLevelId(), levelUpRequest.getAfterLevelId());
        if (b) {
            return new ActivityCheckResultDTO(true, "");
        } else {
            return new ActivityCheckResultDTO(false, eventInfo);
        }
    }

}